// pages/orderCenter/xlzlOrder/list.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
     // 组件参数设置，传递到组件
     defaultData: {
      title: "自在寻麓", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,

    openid: '',
    shareTitle: '',
    shareImg: '',
    position:0,
    list:[],//项目列表
    minuteTotal:0,//可以选择的总时长
    mustMinute:0,//必选项的总分钟数
    chooseId:'',
    chooseIdArr:[],
    itemName:'自在寻麓',
  },
  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  //选择
  choose(e){
   
    let that = this;
    let list = that.data.list;
    let index = e.currentTarget.dataset.index;
    let parentIndex = e.currentTarget.dataset.parentindex;
    let mustMinute = that.data.mustMinute;
    let timeTotal = 0;
    //单选
    if(list[parentIndex].question_type==1){
      if(list[parentIndex].chooseList.length>1){
        for(var i = 0;i<list[parentIndex].chooseList.length;i++){
          list[parentIndex].chooseList[i].checked = false;
        }
      }
      list[parentIndex].chooseList[index].checked=!list[parentIndex].chooseList[index].checked;
     
      
    }else if(list[parentIndex].question_type==2){
      //多选
      list[parentIndex].chooseList[index].checked=!list[parentIndex].chooseList[index].checked;
    }else{
      list[parentIndex].chooseList[index].checked=true;
    }
    that.setData({
      list
    })
    for(var i= 0;i<list.length;i++){
      for(var j= 0;j<list[i].chooseList.length;j++){
        if(list[i].chooseList[j].checked){
          timeTotal = timeTotal+parseInt(list[i].chooseList[j].minute);

        }
        
      }
    }

    that.setData({
      mustMinute:timeTotal
    })
    console.log(timeTotal)
  },


  sure(){
    let that= this;
    let mustMinute = that.data.mustMinute;
    let minuteTotal = that.data.minuteTotal;
    var list = that.data.list;

    if(mustMinute==0){
      app.showTip("请选择要参观的项目");
      return;
    }if(mustMinute>minuteTotal){
      app.showTip("您所选的参观项目超时");
      return;
    }
    
    var obj = [];
    var row=0;
    //拼接选择的项目
    for(var i=0;i<list.length;i++){
      var chooseList= list[i]['chooseList'];
      for(var j=0;j<chooseList.length;j++){
          if(chooseList[j]['checked']){
            var mid = list[i]['id'];
            var cid = chooseList[j]['id'];
            var chooseObj = {'mid':mid,'cid':cid};
            obj.push(chooseObj);
          }
      }
    }
    wx.setStorageSync('chooseObj', obj)
    wx.setStorageSync('chooseMinute', mustMinute);
    wx.navigateTo({
      url: '/pages/orderCenter/xlzlOrder/info?position='+that.data.position
    });
  },
   //预览图片
   previewImage: function (e) {
    var current = e.target.dataset.src;
    var list = e.target.dataset.list;
    console.log(list)
    let newlist = [];
    //提取数组的部分元素组成一个新数组
    list.forEach(item => {
        newlist.push(app.baseUrl+'admin/'+item.img_path)
    });
    
    wx.previewImage({
      current: current, //当前点击的图片链接
      urls: newlist //图片数组
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;

    var position = options.position;
    console.log(position)
    if(position==2){
      that.setData({
        itemName:'自在寻麓',
        ['defaultData.title']:'自在寻麓'
      })
    }
    if(position==3){
      that.setData({
        itemName:'奢享麓游',
        ['defaultData.title']:'奢享麓游'
      })
    }
    if(position!=1&&position!=2&&position!=3){
      app.showTip();
      setTimeout(function(){
        wx.navigateBack({
          delta: 1
        });
      },1500)
      return;
    }else{
      that.setData({
        position:position
      });
    }

    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');

    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function(res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function(res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getXlListPageInfo(res.data.openid,postion);
              },
              fail: function(res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getXlListPageInfo(openid,position);
    }

    //调用分享
    that.getShareInfo();
  },

  //获取本页数据
  getXlListPageInfo(openid,position) {
    var that = this;

    wx.showLoading({
      title: '数据加载中...'
    });

    wx.request({
      url: that.data.baseUrl + 'getXlListPageInfo.php',
      method: 'POST',
      data: {
        openid: openid,
        position:position
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //后台请求成功
          that.setData({
             list:res.data.list,
             minuteTotal:res.data.minuteTotal,
             mustMinute:res.data.mustMinute
          });
        } else {
          //后台请求失败
          app.showTip(res.data.msg);
          setTimeout(function(){
            wx.redirectTo({
              url: '/pages/orderCenter/index'
            })
          },1500)
          return;
        }
      },
      fail: function(res) {
        wx.hideLoading();
      }
    });
  },


    //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo_order.php',
      method: 'POST',
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function(res) {}
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/orderCenter/index',
      imageUrl: that.data.shareImg,
      success: function(res) {

      },
      fail: function(res) {

      }
    }
  }
})